package tech.lithum.mm.ui.dashboard;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import java.util.ArrayList;
import java.util.List;

import tech.lithum.mm.adapter.Topic;
import tech.lithum.mm.adapter.TopicAdapter;
import tech.lithum.mm.adapter.User;
import tech.lithum.mm.adapter.UsersAdapter;
import tech.lithum.mm.databinding.FragmentDashboardBinding;

public class DashboardFragment extends Fragment {

    private FragmentDashboardBinding binding;

    private TopicAdapter topicAdapter;

    private static String TAG = "mm";

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        topicAdapter = new TopicAdapter(getContext(), new ArrayList<>());

        final ListView listView = binding.listView;

        listView.setFriction(ViewConfiguration.getScrollFriction() * 10);

        listView.setAdapter(topicAdapter);
        dashboardViewModel.getTopics().observe(getViewLifecycleOwner(), new Observer<List<Topic>>() {
            @Override
            public void onChanged(List<Topic> topics) {
                Log.i(TAG, "onChanged" + topics.toString());
                topicAdapter.addAll(topics);
            }
        });
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}